package com.henry.chatkit.messages

import com.henry.chatkit.DataChangeType

class MessageProvider : IMessageProvider {

    private var mDataSource = mutableListOf<MessageInfo>()
    private var mAdapter: MessageListAdapter? = null

    override fun setAdapter(adapter: MessageListAdapter?) {
        mAdapter = adapter
    }

    override fun getDataSource(): MutableList<MessageInfo> {
        return mDataSource
    }

    override fun addMessage(messageInfo: MessageInfo?): Boolean {
        if (messageInfo == null) {
            updateAdapter(DataChangeType.LOAD, 0)
            return true
        }
        if (checkExist(messageInfo)) {
            return true
        }
        val flag = mDataSource.add(messageInfo)
        updateAdapter(DataChangeType.ADD_BACK, 1)
        return flag
    }

    override fun deleteMessage(messages: MessageInfo): Boolean {
        for (i in mDataSource.indices) {
            if (mDataSource[i].id == messages.id) {
                mDataSource.removeAt(i)
                updateAdapter(DataChangeType.DELETE, i)
                break
            }
        }
        return false
    }

    override fun resendMessage(messageInfo: MessageInfo): Boolean {
        var found = false
        for (i in mDataSource.indices) {
            if (mDataSource[i].id == messageInfo.id) {
                remove(i)
                found = true
                break
            }
        }
        return if (!found) {
            false
        } else {
            addMessage(messageInfo)
        }
    }

    override fun updateMessage(messageInfo: MessageInfo): Boolean {
        for (i in mDataSource.indices) {
            if (mDataSource[i].id == messageInfo.id) {
                mDataSource.removeAt(i)
                mDataSource.add(i, messageInfo)
                updateAdapter(DataChangeType.UPDATE, i)
                return true
            }
        }
        return false
    }

    override fun addMessageList(messages: List<MessageInfo>, front: Boolean): Boolean {
        if (messages.isEmpty()) {
            updateAdapter(DataChangeType.LOAD, 0)
            return true
        }
        val flag: Boolean
        if (front) {
            flag = mDataSource.addAll(0, messages)
            updateAdapter(DataChangeType.ADD_FRONT, messages.size)
        } else {
            flag = mDataSource.addAll(messages)
            updateAdapter(DataChangeType.ADD_BACK, messages.size)
        }
        return flag
    }

    override fun deleteMessageList(messages: List<MessageInfo>): Boolean {
        for (i in mDataSource.indices) {
            for (j in messages.indices) {
                if (mDataSource[i].id == messages[j].id) {
                    mDataSource.removeAt(i)
                    updateAdapter(DataChangeType.DELETE, i)
                    break
                }
            }
        }
        return false
    }

    override fun updateMessageList(messages: List<MessageInfo>): Boolean {
        return false
    }

    private fun updateAdapter(type: Int, data: Int) {
        mAdapter?.notifyDataSourceChanged(type, data)
    }

    private fun checkExist(msg: MessageInfo?): Boolean {
        if (msg == null) {
            return false
        }
        val msgId: String = msg.id
        for (i in mDataSource.indices.reversed()) {
            // TODO
            if (mDataSource[i].id == msgId) {
                return true
            }
        }
        return false
    }

    override fun remove(index: Int) {
        mDataSource.removeAt(index)
        updateAdapter(DataChangeType.DELETE, index)
    }



}